<template>
   <div class="dialog">
    <el-dialog
      :title="title"
      :visible.sync="visible"
      :close-on-click-modal="false"
      width="60%"
      top="20vh"
      @close="closeDialog"
    >
   <div style="height: 200px">
      <el-table
        ref="tblMain"
        style="margin-top: 20px"
        :data="listData"
        height="100%"
        @selection-change="handleSelectionChange"
        v-loading.body="loading"
        border
        stripe
      >
        <el-table-column
          type="selection"
          width="50"
          align="center"
        ></el-table-column>
        <el-table-column
          type="index"
          width="50"
          label="序号"
          align="center"
        ></el-table-column>
         <el-table-column label="操作" align="center" width="80px" v-if="rowData.state == '0'||rowData.state == '3'">
          <template slot-scope="scope">
            <el-button  type="text" @click="deleteEvent(scope.row)">删除</el-button>
          </template>
        </el-table-column>
        <el-table-column prop="handoverContent" label="交接内容" align="center">
          <template slot-scope="scope">
            <span >{{ scope.row.handoverContent }}</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="handoverState"
          label="是否完成"
          align="center"
        >
        <template slot-scope="scope">
          <el-radio-group v-model="scope.row.handoverState" :disabled="rowData.state == '1'||rowData.state == '2'">
            <el-radio :label="'1'">是</el-radio>
            <el-radio :label="'0'">否</el-radio>
          </el-radio-group>
          </template>
        </el-table-column>
        <el-table-column
          prop="handoverDesc"
          label="交接描述"
          align="center"
        >
        <template slot-scope="scope">
          <el-input
            v-model.trim="scope.row.handoverDesc"
            placeholder="请输入交接描述"
            :disabled="rowData.state == '1'||rowData.state == '2'"
          ></el-input>
        </template>
        </el-table-column>
      </el-table>
    </div>
    <div slot="footer" class="dialog-footer">
      <el-button @click="closeDialog()">关 闭</el-button>
      <el-button
        type="primary"
        @click="confirmData('createForm')"
        v-if="title != '申请详情'"
        >保 存</el-button
      >
    </div>
  </el-dialog>
  </div>
</template>
<script>
import {
  getStrHodListByApplyId,
  batchRemove
} from '@/api/businessCo'
export default {
  name: 'applyMain',
  props: {
    title: {
      type: String,
      default: ''
    },
    rowData:{
      type: Object,
      default: {}
    }
  },
  data() {
    return {
      visible:true,
      loading: false,
      listData: [],
      selection: [],
    }
  },
  created() {},
  mounted() {
    this.getStrHandoverList()
  },
  methods: {
    confirmData(formName) {
      this.$emit('ok', this.listData)
    },
    closeDialog() {
      this.$emit('cancle')
    },
     /**
     * 多选
     */
     handleSelectionChange(val) {
      this.selection = val
    },
     /**
     * 删除
     */
    deleteEvent(row) {
      let ids = []
      if(row == '1'){
         if (this.selection.length < 1) {
          this.$message.warning('请至少选择一条数据')
          return
        }
        ids = this.selection.map(item=>{
          return item.id
        })
      }else{
        ids.push(row.id)
      }
      this.$confirm('此操作将删除该交接项, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          let data = {
            ids:ids
          }
          batchRemove(data).then((res) => {
            const { code = undefined, data = {} } = res.data
            if (!code) {
              this.$message({
                type: 'success',
                message: '操作成功!'
              })
              this.getSelfApplyList();
            } else {
              this.$message({
                type: 'error',
                message: '操作失败!'
              })
            }
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
     /**
     * 设置初始化值
     */
    initTable(val){
      this.listData = val;
    },
   /**
     * 获取码值
     */
    formatterFiled(cellValue,key,val,list) {
      let item = list.find((item) => item[key] === cellValue)
      return item ? item[val] : ''
    },
    /**
     * 获取交接项列表信息
     */
    getStrHandoverList() {
      let data={
        applyId:this.rowData.id
      }
      getStrHodListByApplyId(data).then(res=>{
        const { code = undefined, data = {} } = res.data
        if (!code) {
          this.loading = false
          this.listData = data;
        } else {
          this.$message.error(data)
        }
        }).catch(() => {
          this.$message.error('服务器繁忙')
          this.loading = false
        })
    },
  }
}
</script>
